package pers.xx.sbm.service.serviceImpl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.enums.ResultEnum;
import pers.xx.sbm.service.LoginService;
import pers.xx.sbm.utils.Md5Utils;
import pers.xx.sbm.vo.ResResult;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：xiexing
 * @description：登录接口实现类
 * @date ：2020/10/10 17:56
 */
@Service
public class LoginServiceImpl implements LoginService {
    /**
     * @description slf4j日志
     * @author XieXing
     * @date 2020/10/12 9:13
     */
    private final static Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);

    /**
     * @description 登录实现方法
     * @author XieXing
     * @date 2020/10/10 17:58
     */
    @Override
    public ResResult login(User user, String code) {
        ResResult result = new ResResult();
        result.setOptName("登陆");
        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getUserPassword())) {
            result.setResult(ResultEnum.PARAM_ERR, "请输入手机号和密码！");
            return result;
        }
        if (StringUtils.isEmpty(code)) {
            result.setResult(ResultEnum.PARAM_ERR, "验证码为空");
            return result;
        }
        HttpServletRequest request = getRequest();//获取request对象
        String verifyCode = (String) request.getSession().getAttribute("code");
        if (!code.equals(verifyCode)) {
            result.setResult(ResultEnum.PARAM_ERR, "验证码错误");
            return result;
        }
        //认证信息
        String password = Md5Utils.getMd5Str(user.getUserPassword());
        //创建token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                user.getUserName(),
                password
        );
        Subject subject = SecurityUtils.getSubject();
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
            result.setResult(ResultEnum.SUCCESS, "验证成功");
        } catch (IncorrectCredentialsException ie) {
            logger.error("手机号或密码不正确！", "手机号或密码不正确");
            result.setResult(ResultEnum.PARAM_ERR, "手机号或密码不正确");
        } catch (UnknownAccountException ue) {
            logger.error(ue.getMessage());
            result.setResult(ResultEnum.PARAM_ERR, ue.getMessage());
        } catch (DisabledAccountException de) {
            logger.error(de.getMessage());
            result.setResult(ResultEnum.PARAM_ERR, de.getMessage());
        }
        return result;
    }

    /**
     * @description 获取request方法
     * @author XieXing
     * @date 2020/10/12 9:12
     */
    public HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        return attributes.getRequest();
    }
}
